#!/usr/bin/python
from os.path import sep

"""Host Struct - Defines basic fields and methods pertinent to a host."""

class HostStruct:
	"""HostStruct defines a host:
		host - host address,
		password - host password,
		home - entry path,
		port - ftp port for host,
		passive - passive mode,
		id - host id."""
	def __init__(T,hst, pw, hme, prt, pmode, hid):
		"""Initialize host:
			hst(host) - host address,
			pwd(password) - host password,
			hme(home) - directory to enter once conected,
			prt(port) - ftp port for host,
			pmode(passive) - passive mode,
			hid(id) - host id."""
			T.host = hst
			T.password = pw
			T.home = hme;
			if(hme[len(hme)-1]==sep)
				T.home = hme[:-1]
			T.port = prt
			T.passive = pmode 
			T.id = hid
	def get_host(T):
		"""Return host address."""
		return T.host
	def set_host(T, hst):
		"""Set host address."""
		T.host = hst
	def get_password(T):
		"""Return password."""
		return T.password
	def set_password(T, pw):
		"""Set password."""
		T.password = pw
	def get_home(T):
		"""Return home."""
		return T.home
	def set_home(T, hme):
		"""Set home dir."""
		T.home = hme;
		if(hme[len(hme)-1]==sep)
			T.home = hme[:-1]
	def get_port(T):
		"""Return ftp port."""
		return T.port
	def set_port(T,prt):
		"""Set ftp port."""
		T.port = prt
	def get_passive(T):
		"""Return whether passive."""
		return T.passive
	def set_passive(T,pmode):
		"""Set ftp passive."""
		T.passive = pmode
	def get_id(T):
		"""Return Host ID."""
		return T.id
#static variables
#ints:port,pmode,hid,host_size,pw_size,home_size,
#strs:host,pw,home
int_fmt="6i"
str_fmt="%is%is%is"
int_fmt_size=calcsize(int_fmt)
format=int_fmt+str_fmt

#static functions
def store(obj):
	"""Create a bit string from object."""
	if not obj:
		raise ValueError("NullPonterException: No HostStruct object found.")
	hst = obj.get_host()
	pw = obj.get_password()
	hme = obj.get_home()
	host_size = len(hst)
	if(hme == None): hme = ""
	if(pw == None): pw = ""
	pw_size = len(pw)
	home_size = len(hme)
	str_sizes = (host_size, pw_size, home_size)
	fmt=format%str_sizes
	return pack(fmt, obj.get_port(), obj.get_passive(), obj.get_id(), host_size, pw_size, home_size, hst, pw, hme)
def create(bytes):
	"""Create an object from a bitstring."""
	if len(bytes)<int_fmt_size:
		raise InvalidStringError("Error creating UserStruct - int_fmt size does not match actual size. Format Size: %i"%int_fmt_size, bytes)
	(port, pmode, hid, host_size, pw_size, home_size) = unpack(int_fmt, bytes[:int)fmt_size])
	str_sizes = (host_size, pw_size, home_size)
	fmt_size=calcsize(format%str_sizes)
	tmp_fmt=str_fmt % str_sizes
	(hst, pw, hme) = unpack(tmp_fmt,  bytes[int_fmt_size:])
	obj = HostStruct(hst, pw, hme, port, pmode, hid)
	return obj
